﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

namespace Spore.Utils
{
  public  class SerializationHelper
    {
      public static byte[] GetBytes(object obj)
      {
          IFormatter fp = new BinaryFormatter();
          MemoryStream ms = new MemoryStream();
          fp.Serialize(ms, obj);
          return ms.ToArray();
      }

      public static object GetValue(byte[] data,int offset,int count)
      {
          IFormatter fp = new BinaryFormatter();
          MemoryStream ms = new MemoryStream();
         ms.Write(data,offset,count);
         ms.Position = 0;
          return fp.Deserialize(ms);
      }

      public static T GetValue<T>(byte[] data, int offset, int count)
      {
          IFormatter fp = new BinaryFormatter();
          MemoryStream ms = new MemoryStream();
          ms.Read(data, offset, count);
          return (T)fp.Deserialize(ms);
      }

     
    }
}
